package com.ruoyi.patient.utils;

//import com.dhcc.util.biz.BizMethodConstant;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
//import com.sun.tools.classfile.StackMapTable_attribute.same_frame;

/**
 * @author Administrator
 *
 */
public class DesUtil {

	public static String encrypt(String encryptString, String key) {

		try {
			byte[] raw = key.getBytes("utf-8");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
			byte[] encrypted = cipher.doFinal(encryptString.getBytes("utf-8"));
			return new String(Base64.getEncoder().encode(encrypted), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
		
	}

	public static String decrypt(String decryptString, String key) {
		try {
			byte[] raw = key.getBytes("utf-8");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, skeySpec);
			byte[] encrypted1 = Base64.getDecoder().decode(decryptString);
			byte[] original = cipher.doFinal(encrypted1);
			String originalString = new String(original,"utf-8");
			return originalString;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
    }
	
	public static void main(String[] args) {
		//System.out.println(decrypt("gPHF/hppxMB+Xy8w0c139kybyOE2iaIyZ5GXiIuuQm2rY+9WO+ZkwxTtTCTrpZaKO09QaDqsNFeHrbfaoKvr10B6urbC/mLa2ikOA0TunjHVNFyzUXPUuf3vRfPjtwPBzF7g8HcWBE3B80vuu5L0e0SPNPwHI0y9L8Vx20EGfnXMQTVj8fI2SS7Y4B5BfTK8owNHa8OKcv8yzxs8LiT5+sCedG0UKu5yMaxIloZ7W/KXdk927GRgEf8doSGeG4WgyDMTGGCirzhbKzyLK0ctTy3ygmthBmHiGSTYQ/AOUdGlzcvywcbWgYryfczrHrqt7UskSw5op72WC4pDWOp+T4ueYcGnAcWNCxvybAfDEcFSQuG3PAJXZllwhnPL6hD7owNHa8OKcv8yzxs8LiT5+sbmBT5Fo1+QjWC4eAPxuJD8JE/5DvgB1cu9BKn4vh6GX4dyh/v5vpviSmxHnKOHrxkQxV1Xr5+IBNSxdxZlxKAWihrZUjMw8GYs5Mja6cwydm2XLzzmw2V2KhKkdRSPyngvDvmZL1voLWiF4XYLZleuiYrfqDJ2oy73FaXR/uuZypMgN6FYsRBL6d3wjIX6Of5z8jA9U0WEP8iy7VNuyhdNihlz34vSIad3DI99iTJfk8OkdMswK/G7b6kj8A53HOE8E/+hPnkm71wqAbup/Tu5RvuApuJQ/idQCdc9l3sCIACsTBueOrQCBEKR9GYK5htVG3PYZIYNrmV9Wsry8Ho=", BizMethodConstant.ENCRYPTKEY));
	}
}
